package com.txunda.zhuohired.aty.manage.my;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.ants.theantsgo.gson.GsonUtil;
import com.ants.theantsgo.tools.AlertDialog;
import com.ants.theantsgo.util.L;
import com.ants.theantsgo.view.pulltorefresh.PullToRefreshBase;
import com.ants.theantsgo.view.pulltorefresh.PullToRefreshListView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.adapter.manage.my.ManageResumeCollectAdapter;
import com.txunda.zhuohired.base.BaseAty;
import com.txunda.zhuohired.bean.manage.my.ManageResumeCollectBean;
import com.txunda.zhuohired.http.pst.UserPst;

import java.util.List;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/3/14 14:05
 * 功能描述：简历收藏
 * 联系方式：jiahuanchen@126.com
 */

public class ManageResumeCollectAty extends BaseAty {

    /**
     * 标题
     */
    @ViewInject(R.id.tv_layout_white_center)
    private TextView tv_layout_white_center;
    /**
     * 编辑
     */
    @ViewInject(R.id.tv_layout_white_right)
    private TextView tv_layout_white_right;
    /**
     * listView
     */
    @ViewInject(R.id.lv_manage_resume_collect)
    private PullToRefreshListView lv_manage_resume_collect;


    private ManageResumeCollectAdapter manageResumeCollectAdapter;

    private int collectStatus = 1;

    private UserPst userPst;
    private String token;
    private int page = 1;
    private String uid;//简历收藏id
    private List<ManageResumeCollectBean.DataBean> manageResumeCollectList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showStatusBar(R.id.layout_white_head);
        tv_layout_white_center.setText("简历收藏");
        tv_layout_white_right.setVisibility(View.VISIBLE);
        tv_layout_white_right.setText("编辑");

        lv_manage_resume_collect.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 1;
                initHttp();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                initHttp();
            }
        });
    }

    @Override
    @OnClick({R.id.tv_layout_white_right})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_layout_white_right://编辑
                if (collectStatus == 1) {
                    collectStatus = 2;
                    tv_layout_white_right.setText("完成");
                    initAdapter();
                } else {
                    collectStatus = 1;
                    tv_layout_white_right.setText("编辑");
                    initAdapter();
                }
                break;
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_manage_resume_collect;
    }

    @Override
    protected void initialized() {
        userPst = new UserPst(this);
        token = application.getUserInfo().get("data");
    }

    @Override
    protected void requestData() {
        initHttp();
//        initAdapter();
    }
    private void initHttp(){
        userPst.collectList(token,String.valueOf(page));
    }

    private void initAdapter() {
        manageResumeCollectAdapter = new ManageResumeCollectAdapter(this, collectStatus,manageResumeCollectList);
        lv_manage_resume_collect.setAdapter(manageResumeCollectAdapter);
        manageResumeCollectAdapter.SetOnClickListener(new ManageResumeCollectAdapter.HolderClickListener() {
            @Override
            public void onSureHolderClick(int position) {
                onShowDialog(manageResumeCollectList.get(position).getId());
                uid = manageResumeCollectList.get(position).getId();
            }
        });
    }

    private void onShowDialog(final String id) {
        new AlertDialog(this).builder().setTitle("提示").setMsg("确认删除该条收藏?")
                .setPositiveButton("确认", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        //                        L.e("======id======",id);
                        //                        L.e("======model======",model);
                        userPst.delCollect(token,uid);
                        showToast("删除成功");
                        initHttp();
                    }
                }).setNegativeButton("取消", new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub



            }
        }).show();
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        if(requestUrl.contains("collectList")){
            L.e("=========",jsonStr);
            lv_manage_resume_collect.onRefreshComplete();
            ManageResumeCollectBean manageResumeCollectBean = GsonUtil.GsonToBean(jsonStr,ManageResumeCollectBean.class);

            manageResumeCollectList = manageResumeCollectBean.getData();


            initAdapter();

        }
    }


}
